package es.aa.android.cartomatic.appview.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import es.aa.android.cartomatic.R;
import es.aa.android.cartomatic.appview.DeckDetailView;
import es.aa.android.cartomatic.domain.Deck;
import es.aa.android.cartomatic.implementations.service.NavigationService;
import es.aa.android.cartomatic.interfaces.service.INavigationService;

public class DeckAdapter extends BaseAdapter {


    private Context context;
    private List<Deck> decks;
    private boolean showImage = true;
    private boolean clickable = true;
    private LayoutInflater li;
    private INavigationService navService;
    
    public DeckAdapter(Context context, List<Deck> decks) {
        this.context = context;
        this.decks = decks;
        this.li = (LayoutInflater) ((Activity) context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.navService = new NavigationService();
    }

    public DeckAdapter(Context context, List<Deck> decks, boolean showImage, boolean clickable) {
        this.context = context;
        this.decks = decks;
        this.showImage = showImage;
        this.clickable = clickable;
        this.li = (LayoutInflater) ((Activity) context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.navService = new NavigationService();
    }

    @Override
    public int getCount() {
        return decks.size();
    }

    @Override
    public Object getItem(int i) {
        return decks.get(i);
    }

    @Override
    public long getItemId(int i) {
        return decks.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = li.inflate(R.layout.card, null);
            holder = new ViewHolder();

            holder.title = (TextView) view.findViewById(R.id.textViewTitle);
            holder.description = (TextView) view.findViewById(R.id.textViewSubtitle);
            holder.image = (ImageView) view.findViewById(R.id.imageView);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        Deck d = decks.get(i);
        holder.title.setText(d.getName());
        holder.description.setText(d.getDescription());
        
        if (showImage) {
        	holder.image.setImageBitmap(d.getImage());
        } else {
        	holder.image.setVisibility(View.GONE);
        	holder.description.setVisibility(View.GONE);
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	if (clickable) {
            		navService.navigate((Activity) context, DeckDetailView.class);
            	}
            }
        });

        return view;
    }

    static class ViewHolder {
        private TextView title;
        private TextView description;
        private ImageView image;
    }
    
    public void showImage(boolean show) {
    	this.showImage = show;
    }
    
    public void setClickcable (boolean clickable) {
    	this.clickable = clickable;
    }
    
}
